import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import('../views/login/Login')
const ObstacleMgt = () => import('../views/obstacleMgt/ObstacleMgt')
const PersonMgt = () => import('../views/personMgt/PersonMgt')
const Main = () => import('../views/main/Main')
const Safe = () => import('../views/safe/Safe')

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  if(typeof(location)=="string"){
    var Separator = "&";
    if(location.indexOf('?')==-1) { Separator='?'; }
    location = location + Separator + "random=" + Math.random();
  }
  return routerPush.call(this, location).catch(error=> error)
}



Vue.use(VueRouter)

const routes = [
  {
    path: '*',
    redirect: '/obstaclemgt',
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '课室管理系统-登录'
    },
    component: Login
  },
  {
    path: '/main',
    name: 'main',
    component: Main,
    meta: {
      title: '课室管理系统-首页'
    },
    children: [
      {
        path: '/obstaclemgt',
        component: ObstacleMgt
      },
      {
        path: '/personmgt',
        component: PersonMgt
      },
      {
        path: '/safe',
        component: Safe
      }
    ]
  },

]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  const isLogin = !!localStorage.eleToken
  if (to.path === '/login') {
    next()
  } else {
    isLogin ? next() : next('/login')
  }
})

export default router
